using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.SceneManagement;
using System;
using UnityEngine.UI;

namespace Super
{
	public static class FWGameUtil
	{
		public static Transform GetSceneRoot(string name, string addr = "")
		{
			Transform tf = DataHelperUtil.FindDontDestroyObject(name)?.transform;
			if (tf != null) return tf;

			GameObject[] gameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
			foreach (GameObject gameObject in gameObjects)
			{
				if (gameObject.name == name)
				{
					return gameObject.transform;
				}
			}

			if (addr != "")
			{
				GameObject prefab = AssetUtil.LoadAsset<GameObject>(addr);
				GameObject go = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity);
				go.name = name;
				return go.transform;
			}

			return null;
		}

		public static void MainCameraStackAdd(Camera mainCamera, Camera camera)
		{
			UniversalAdditionalCameraData cameraURPData = mainCamera.GetUniversalAdditionalCameraData();
			List<Camera> cameraStack = cameraURPData.cameraStack;
			if (cameraStack.Contains(camera)) return;
			cameraStack.Add(camera);
		}

		public static int GetSymbolIntH(GameObject user, GameObject other)
		{
			float offset = user.transform.position.x - other.transform.position.x;
			if (offset > 0)
			{
				return -1;
			}
			return 1;
		}

		public static int GetSymbolIntV(GameObject user, GameObject other)
		{
			float offset = user.transform.position.y - other.transform.position.y;
			if (offset > 0)
			{
				return -1;
			}
			return 1;
		}

		public static int CalculateGridItemsPerRow(GridLayoutGroup gridLayout)
		{
			RectTransform rectTransform = gridLayout.GetComponent<RectTransform>();

			float gridWidth = rectTransform.rect.width;
			float spacing = gridLayout.spacing.x;
			float itemWidth = gridLayout.cellSize.x + spacing;

			float availableWidth = gridWidth - gridLayout.padding.left - gridLayout.padding.right + spacing;
			int itemsPerRow = Mathf.FloorToInt(availableWidth / itemWidth);

			return itemsPerRow;
		}

		public static int CalculateItemsPerColumn(GridLayoutGroup gridLayout, float gridHeight)
		{
			RectTransform rectTransform = gridLayout.GetComponent<RectTransform>();

			float spacing = gridLayout.spacing.y;
			float itemHeight = gridLayout.cellSize.y + spacing;

			float availableWidth = gridHeight - gridLayout.padding.top - gridLayout.padding.bottom + spacing;
			int itemsPerColumn = Mathf.FloorToInt(availableWidth / itemHeight);

			return itemsPerColumn;
		}

		public static void SetScrollVerticalProgress(ScrollRect scrollRect, float verticalProgress)
		{
			verticalProgress = 1 - Mathf.Clamp01(verticalProgress);

			scrollRect.normalizedPosition = new Vector2(scrollRect.normalizedPosition.x, verticalProgress);
		}

		public static void SetScrollHorizontalProgress(ScrollRect scrollRect, float horizontalProgress)
		{
			horizontalProgress = Mathf.Clamp01(horizontalProgress);

			scrollRect.normalizedPosition = new Vector2(horizontalProgress, scrollRect.normalizedPosition.y);
		}

		public static void SetImageGray(Image image, bool isGray)
		{
			if (!isGray)
			{
				image.material = null;
				return;
			}
			Material material = AssetSettingSO.Ins.UIGrayMaterial;
			image.material = material;
		}
	}
}
